#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/**
 * Definition for singly-linked list.
 */
struct ListNode {
	int val;
	struct ListNode* next;
};
typedef struct ListNode listNode, *listnode;

struct ListNode* rotateRight(struct ListNode* head, int k);
int listlen(listnode head);

int main()
{
	return 0;
}

struct ListNode* rotateRight(struct ListNode* head, int k)
{
	int length = listlen(head);
	if (!length) return NULL;
	k %= length;
	if (!k || k == length) return head;

	listNode aux = { 0, head }, *p = &aux;
	for (int i = length - k; i > 0; i--) p = p->next;

	listnode ins = p->next;
	p->next = NULL;
	listnode temp = aux.next;
	aux.next = ins;
	while (ins->next) ins = ins->next;
	ins->next = temp;

	return aux.next;
}

int listlen(listnode head)
{
	int size = 0;
	while (head) {
		head = head->next;
		size++;
	}
	return size;
}
